
import pymysql
import configparser
from DBUtils.PooledDB import PooledDB

class Tohtml(object):
    def __init__(self):
        self.config_file = r'conf\config.ini'
        self.config = configparser.ConfigParser()
        self.config.read(self.config_file, encoding="utf-8")
        self.mysql_con()

    def mysql_con(self):
        host = self.config.get('mysql', 'host')
        dbname = self.config.get('mysql', 'dbname')
        user = self.config.get('mysql', 'username')
        pwd = self.config.get('mysql', 'passwd')

        db = pymysql.connect(host, user, pwd, dbname, charset='utf8')
        cursor = db.cursor()
        return cursor

class Mysql(object):
    __instance = None

    def __new__(cls, *args, **kwargs):
        if cls.__instance is None:
            cls.__instance = super(Mysql, cls).__new__(cls, *args, **kwargs)
        return cls.__instance

    def __init__(self):
        self.config_file = r'conf\config.ini'
        self.config = configparser.ConfigParser()
        self.config.read(self.config_file, encoding="utf-8")
        _host = self.config.get('mysql', 'host')
        _dbname = self.config.get('mysql', 'dbname')
        _user = self.config.get('mysql', 'username')
        _pwd = self.config.get('mysql', 'passwd')
        self.mysql = PooledDB(creator=pymysql, mincached=10, maxcached=20,
                              host=_host, port=3306, user=_user, passwd=_pwd,
                              db=_dbname, charset="utf8")

    def getAll(self, sql):
        _conn = self.mysql.connection()
        _cursor = _conn.cursor()
        _cursor.execute(sql)
        result = _cursor.fetchall()
        _cursor.close()
        _conn.close()
        return result

    def getOne(self, sql):
        _conn = self.mysql.connection()
        _cursor = _conn.cursor()
        _cursor.execute(sql)
        result = _cursor.fetchone()
        _cursor.close()
        _conn.close()
        return result

class InfoMysql(object):
    __instance = None

    def __new__(cls, *args, **kwargs):
        if cls.__instance is None:
            cls.__instance = super(InfoMysql, cls).__new__(cls, *args, **kwargs)
        return cls.__instance

    def __init__(self):
        self.config_file = r'conf\config.ini'
        self.config = configparser.ConfigParser()
        self.config.read(self.config_file, encoding="utf-8")
        _host = self.config.get('mysql', 'host')
        _dbname = self.config.get('mysql', 'dbname_info')
        _user = self.config.get('mysql', 'username')
        _pwd = self.config.get('mysql', 'passwd')
        self.mysql = PooledDB(creator=pymysql, mincached=10, maxcached=20,
                              host=_host, port=3306, user=_user, passwd=_pwd,
                              db=_dbname, charset="utf8")

    def getAll(self, sql):
        _conn = self.mysql.connection()
        _cursor = _conn.cursor()
        _cursor.execute(sql)
        result = _cursor.fetchall()
        _cursor.close()
        _conn.close()
        return result

    def getOne(self, sql):
        _conn = self.mysql.connection()
        _cursor = _conn.cursor()
        _cursor.execute(sql)
        result = _cursor.fetchone()
        _cursor.close()
        _conn.close()
        return result